﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace FirstHomeWork.Utility.Extend
{
    public static class AbstractValidateAttributeExtend
    {
        /// <summary>
        /// 验证实体是否符合要求
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool Validata<T>(this T t) where T : class, new()
        {
            Type type = typeof(T);
            foreach (PropertyInfo property in type.GetProperties())
            {                
                if (property.IsDefined(typeof(AbstractValidateAttribute),true))
                {
                    object ovalue = property.GetValue(t);

                   var abstractValidateAttributeList= property.GetCustomAttributes<AbstractValidateAttribute>();
                    foreach (var item in abstractValidateAttributeList)
                    {
                        if (!item.Validate(ovalue))
                        {
                            return false;
                        }
                       
                    }
                }
            }
            return true;
        }
    }
}
